﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules;

namespace YT.XH.NOSE
{
    public partial class frmNSCDReturnVisitDetail : Form
    {
        private string mstrSeqNo = string.Empty;
        private string mSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private FormStatus mfrmStatus = new FormStatus();
        private bool mblnExit = false;
        private string mstrTableName = "NoseVisitRecordDtl";

        public frmNSCDReturnVisitDetail(string strSeqNo, string strSickRecID)
        {
            // RecId---> SeqNo
            InitializeComponent();
            mstrSeqNo = strSeqNo;
            mSickRecID = strSickRecID;
            if ( strSeqNo == "" || strSeqNo == "0")
                mfrmStatus = FormStatus.AddNew;
            else
                mfrmStatus = FormStatus.Update;
        }

        public void ShowVist()
        {
            initControl();
            FillContent();
        }

        private void initControl()
        {
            this.Text = "畅迪舌下免疫治疗月回访记录表";
            txtSickName.Text = DataFunctions.GetFieldValue("mSickPerson", "SickName", "SickRecID='" + mSickRecID + "'");
            if (  !BaseFunctions.IsDate ( datVisitDate.Value  ))
                datVisitDate.Value = BaseFunctions.FormatDate(DateTime.Now.Date.ToString(), DateFormatOption.DateFmtYMD);
        }

        private void FillContent()
        {
            if (mstrSeqNo == "" || mstrSeqNo == "0")
            {
                txtSeqNo.Text = (CvtFunc.ToInt(DataFunctions.GetFieldValue(mstrTableName, "max(SeqNo) as SeqNo", " 1=1 and SickRecID='"+ mSickRecID +"'")) + 1).ToString();
                return;
            }
            txtSeqNo.Text = mstrSeqNo;
            string strSQL = string.Empty;
            strSQL = "select * from " + mstrTableName + " where SeqNo='" + mstrSeqNo + "' and SickRecID='"+ mSickRecID +"' ";
            DataRow drDetail = mdbcon.GetDataFirstRow(strSQL);
            if (drDetail != null)
            {
                txtSickName.Text = CvtFunc.ToString(drDetail["SickName"]);
                txtCity.Text = CvtFunc.ToString(drDetail["City"]);
                txtLastWeek_Evaluate.Text = CvtFunc.ToString(drDetail["LastWeek_Evaluate"]);
                trkLastWeek_Evaluate.Value = CvtFunc.ToInt(txtLastWeek_Evaluate.Text);
                datVisitDate.Text = BaseFunctions.FormatDate(drDetail["VisitDate"].ToString(), DateFormatOption.DateFmtYMD);
                txtCurCaseDI.Text = CvtFunc.ToString (drDetail["CurCaseDI"]) ;
                txtCurCaseHAO.Text = CvtFunc.ToString (drDetail ["CurCaseHAO"]) ;
                txtCurCaseTime.Text = CvtFunc.ToString(drDetail["CurCaseTime"]);
                txtSeqNo.Text = CvtFunc.ToString(drDetail["SeqNo"]); 
                string strTempValue = string.Empty;
                #region 鼻塞
                strTempValue = CvtFunc.ToString(drDetail["LastWeek_BS"]);
                if (strTempValue == "无") rad1_1.Checked = true;
                if (strTempValue == "轻度") rad1_2.Checked = true;
                if (strTempValue == "中度") rad1_3.Checked = true;
                if (strTempValue == "重度") rad1_4.Checked = true;
                #endregion
                #region 鼻痒
                strTempValue = CvtFunc.ToString(drDetail["LastWeek_BY"]);
                if (strTempValue == "无") rad2_1.Checked = true;
                if (strTempValue == "轻度") rad2_2.Checked = true;
                if (strTempValue == "中度") rad2_3.Checked = true;
                if (strTempValue == "重度") rad2_4.Checked = true;
                #endregion
                #region 流涕
                strTempValue = CvtFunc.ToString(drDetail["LastWeek_PT"]);
                if (strTempValue == "无") rad3_1.Checked = true;
                if (strTempValue == "轻度") rad3_2.Checked = true;
                if (strTempValue == "中度") rad3_3.Checked = true;
                if (strTempValue == "重度") rad3_4.Checked = true;
                #endregion
                #region 喷嚏
                strTempValue = CvtFunc.ToString(drDetail["LastWeek_PD"]);
                if (strTempValue == "无") rad4_1.Checked = true;
                if (strTempValue == "轻度") rad4_2.Checked = true;
                if (strTempValue == "中度") rad4_3.Checked = true;
                if (strTempValue == "重度") rad4_4.Checked = true;
                #endregion


                #region 治疗评价  
                strTempValue = CvtFunc.ToString(drDetail["CureRank"]);
                if ( strTempValue =="完全控制，基本未发") rad5_1.Checked = true ;
                if (strTempValue == "明显好转") rad5_2.Checked = true;
                if (strTempValue == "稍好转") rad5_3.Checked = true;
                if (strTempValue == "无明显变化") rad5_4.Checked = true;
                if (strTempValue == "症状加重") rad5_5.Checked = true;


                #endregion
                if (CvtFunc.ToBoolean(drDetail["LastWeekUseDrug"]))
                    radLastWeekUseDrug_Yes.Checked = true;
                else
                    radLastWeekUseDrug_No.Checked = true;

                txtUntowardEffect.Text = CvtFunc.ToString(drDetail["UntowardEffect"]);
                txtRemark.Text = CvtFunc.ToString(drDetail["Remark"]); 
                txtOraldrugName.Text = CvtFunc.ToString(drDetail["OraldrugName"]);
                txtOraldrugTimes.Text = CvtFunc.ToString(drDetail["OraldrugTimes"]);
                txtSparydrugName.Text = CvtFunc.ToString(drDetail["SparydrugName"]);
                txtSparydrugTimes.Text = CvtFunc.ToString(drDetail["SparyTimes"]);
                txtOthers.Text = CvtFunc.ToString(drDetail["Others"]);
            }
        }
        private bool ValidCheck()
        {
            string strErrMSG = string.Empty;
            //if (!BaseFunctions.IsDate(datVisitDate.Value))
            //{
            //    strErrMSG = "请输入回访日期。";
            //}
            //else if (txtLastWeek_Evaluate.Text.ToString().Trim() == "")
            //{
            //    strErrMSG = "请输入 视觉模拟评分 ";
            //}
            //else if (!rad1_1.Checked && !rad1_2.Checked && !rad1_3.Checked && !rad1_4.Checked)
            //{
            //    strErrMSG = "请选择 鼻塞 的主要症状。";
            //}
            //else if (!rad2_1.Checked && !rad2_2.Checked && !rad2_3.Checked && !rad2_4.Checked)
            //{
            //    strErrMSG = "请选择 鼻痒 的主要症状。";
            //}
            //else if (!rad3_1.Checked && !rad3_2.Checked && !rad3_3.Checked && !rad3_4.Checked)
            //{
            //    strErrMSG = "请选择 流涕 的主要症状。";
            //}
            //else if (!rad4_1.Checked && !rad4_2.Checked && !rad4_3.Checked && !rad4_4.Checked)
            //{
            //    strErrMSG = "请选择 喷嚏 的主要症状。";
            //}
            //else if (!radLastWeekUseDrug_Yes.Checked && !radLastWeekUseDrug_No.Checked)
            //{
            //    strErrMSG = "你选择最近一周用药情况。";
            //}
            //else if (radLastWeekUseDrug_Yes.Checked == true)
            //{
            //    if ((txtOraldrugName.Text.ToString().Trim() == "") && (txtSparydrugName.Text.ToString().Trim() == "") && txtOthers.Text.ToString().Trim() == "")
            //    {
            //        strErrMSG = "你必须输入药品名称。";
            //    }
            //}
            if (strErrMSG != "")
            {
                MessageBox.Show(strErrMSG, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;

        }
        public bool UpdateDetail( DBAccess dbcon )
        {
            string strSQL = string.Empty;
            DataRow drRow = null;
            string strSeqNo = string.Empty;
            if (!ValidCheck()) return false;
            if (mfrmStatus == FormStatus.Update)
            {
                strSQL = "select top 1 * from " + mstrTableName + " where SeqNo='" + mstrSeqNo + "' and SickRecID='"+ mSickRecID +"' ";
                drRow = dbcon.GetDataFirstRow(strSQL);
                if (drRow == null)
                {
                    mfrmStatus = FormStatus.AddNew;                           
                }                
            }            
            if (mfrmStatus == FormStatus.AddNew)
            {
                drRow = dbcon.GetDataTable("select top 0 * from " + mstrTableName + " ").NewRow();                
            }
            strSeqNo = mstrSeqNo;
            drRow["SickName"] = txtSickName.Text;
            drRow["SeqNo"] =CvtFunc.ToInt ( txtSeqNo.Text ) ;
            drRow["SickRecID"] = mSickRecID;
            drRow["VisitDate"] = BaseFunctions.FormatDate(datVisitDate.Value, DateFormatOption.DateFmtYMD);
            drRow["LastWeekUseDrug"] = (radLastWeekUseDrug_Yes.Checked == true ? 1 : 0);
            drRow["LastWeek_Evaluate"] = CvtFunc.ToInt(txtLastWeek_Evaluate.Text);
            drRow["CurCaseTime"] = CvtFunc.ToInt(txtCurCaseTime.Text);            
            drRow["CurCaseDI"] = CvtFunc.ToString(txtCurCaseDI.Text);
            drRow["CurCaseHAO"] = CvtFunc.ToString(txtCurCaseHAO.Text);
            drRow["City"] = CvtFunc.ToString(txtCity.Text);
            drRow["UntowardEffect"] = CvtFunc.ToString(txtUntowardEffect.Text);
            drRow["Remark"] = CvtFunc.ToString(txtRemark.Text); 

            string strTempValue = string.Empty;
            #region 鼻塞
            if (rad1_1.Checked) strTempValue = rad1_1.Text;
            if (rad1_2.Checked) strTempValue = rad1_2.Text;
            if (rad1_3.Checked) strTempValue = rad1_3.Text;
            if (rad1_4.Checked) strTempValue = rad1_4.Text;
            drRow["LastWeek_BS"] = strTempValue;
            #endregion

            #region 鼻痒
            if (rad2_1.Checked) strTempValue = rad2_1.Text;
            if (rad2_2.Checked) strTempValue = rad2_2.Text;
            if (rad2_3.Checked) strTempValue = rad2_3.Text;
            if (rad2_4.Checked) strTempValue = rad2_4.Text;
            drRow["LastWeek_BY"] = strTempValue;
            #endregion

            #region 流涕
            if (rad3_1.Checked) strTempValue = rad3_1.Text;
            if (rad3_2.Checked) strTempValue = rad3_2.Text;
            if (rad3_3.Checked) strTempValue = rad3_3.Text;
            if (rad3_4.Checked) strTempValue = rad3_4.Text;
            drRow["LastWeek_PT"] = strTempValue;
            #endregion
            #region 喷嚏
            if (rad4_1.Checked) strTempValue = rad4_1.Text;
            if (rad4_2.Checked) strTempValue = rad4_2.Text;
            if (rad4_3.Checked) strTempValue = rad4_3.Text;
            if (rad4_4.Checked) strTempValue = rad4_4.Text;
            drRow["LastWeek_PD"] = strTempValue;
            #endregion

            #region 治疗评价
            if (rad5_1.Checked) strTempValue = rad5_1.Text;
            if (rad5_2.Checked) strTempValue = rad5_2.Text;
            if (rad5_3.Checked) strTempValue = rad5_3.Text;
            if (rad5_4.Checked) strTempValue = rad5_4.Text;
            drRow["CureRank"] = strTempValue;
            #endregion



            if (pnlLastWeekUseDrug.Enabled == true)
            {
                drRow["OraldrugName"] = txtOraldrugName.Text;
                drRow["OraldrugTimes"] = CvtFunc.ToInt(txtOraldrugTimes.Text);
                drRow["SparydrugName"] = txtSparydrugName.Text;
                drRow["SparyTimes"] = CvtFunc.ToInt(txtSparydrugTimes.Text);
                drRow["Others"] = CvtFunc.ToString(txtOthers.Text);
            }
            else
            {
                drRow["OraldrugName"] = "";
                drRow["OraldrugTimes"] = CvtFunc.ToInt("0");
                drRow["SparydrugName"] = "";
                drRow["SparyTimes"] = CvtFunc.ToInt("0");
                drRow["Others"] = "";

            }

            string strFields = @" SeqNo,SickRecID,SickName,VisitDate,LastWeek_Evaluate,
                                  LastWeek_BS,LastWeek_BY,LastWeek_PT,LastWeek_PD,LastWeekUseDrug,OraldrugName,
                                  OraldrugTimes,SparydrugName,SparyTimes,Others,Remark, City,
                                  UntowardEffect,CurCaseTime,CurCaseDI,CurCaseHAO  ";
            if (mfrmStatus == FormStatus.AddNew)
                DataFunctions.UpdateTable(dbcon, strFields, mstrTableName, drRow, FormStatus.AddNew);
            else
                DataFunctions.UpdateTable(dbcon, strFields, mstrTableName, drRow, FormStatus.Update);

            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (!UpdateDetail(mdbcon )) return; 
            }
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void frmNSCDReturnVisitDetail_Load(object sender, EventArgs e)
        {
            
            initControl();
            FillContent();
            timer1.Start();
        }

        private void trkLastWeek_Evaluate_Scroll(object sender, EventArgs e)
        {
            txtLastWeek_Evaluate.Text = trkLastWeek_Evaluate.Value.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!UpdateDetail(mdbcon )) return;
            this.Close();                
        }

        private void radLastWeekUseDrug_No_CheckedChanged(object sender, EventArgs e)
        {
            if (radLastWeekUseDrug_No.Checked == true)
                pnlLastWeekUseDrug.Enabled = false;
            else
                pnlLastWeekUseDrug.Enabled = true;
        }

        private void radLastWeekUseDrug_Yes_CheckedChanged(object sender, EventArgs e)
        {
            if (radLastWeekUseDrug_Yes.Checked == true)
                pnlLastWeekUseDrug.Enabled = true;
            else
                pnlLastWeekUseDrug.Enabled = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void datVisitDate_Load(object sender, EventArgs e)
        {

        }
    }
}
